package com.yswee.mallnavi.view.brand.hor;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.yswee.framework.data.BaseArrayData;
import com.yswee.framework.data.parser.IParser;
import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.net.http.Callback;
import com.yswee.framework.net.http.Entity;
import com.yswee.framework.utils.StringUtils;
import com.yswee.framework.widget.MToast;
import com.yswee.framework.widget.loading.LoadingLayout;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.entity.info.BrandEntity;
import com.yswee.mallnavi.model.info.BrandListModel;
import com.yswee.mallnavi.view.brand.IGridView;

import java.util.ArrayList;

public class IndexView extends LoadingLayout implements IGridView {

    private HListView lstdata;

    private String mCategoryId;
    private String mKeyword;
    private BrandListModel mListModel;

    private IActionCallback mActionCallback;

    private IActionCallback mActionCallbackProxy = new IActionCallback() {
        @Override
        public void onBrandClicked(View v, BrandEntity brand) {
            if (mActionCallback != null) {
                mActionCallback.onBrandClicked(v, brand);
            }
        }
    };

    @Override
    public void setActionCallback(IActionCallback c) {
        mActionCallback = c;
    }

    @Override
    public void setFilter(String filter) {
        mKeyword = filter;
    }

    @Override
    public void setParams(String categoryId) {
        mCategoryId = categoryId;
    }

    @Override
    public void setSelectionKey(String key) {
        lstdata.setSelectionKey(key);
    }

    @Override
    public void refresh() {
        onApplyLoadingData();
    }

    @Override
    public void onDestroy() {
        lstdata.onDestroy();
    }

    @Override
    protected boolean getAutoLoading() {
        return false;
    }

    public IndexView(Context context) {
        super(context);
    }

    public IndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected View getLayoutView() {
        if (lstdata == null) {
            lstdata = new HListView(mContext);
        }
        return lstdata;
    }

    @Override
    protected void onBindListener() {
        super.onBindListener();
    }

    @Override
    protected void onApplyData() {
        super.onApplyData();
    }

    @Override
    public void onApplyLoadingData() {
        getListInfo();
    }

    private void getListInfo() {
        if (mCategoryId == null) {
            return;
        }
        if (mListModel == null) {
            ITaskContext context = mContext instanceof ITaskContext ?
                    (ITaskContext) mContext : null;
            mListModel = new BrandListModel(context);
        }
        mListModel.setParams(mCategoryId);
        mListModel.setKeyword(mKeyword);
        gotoLoading();
        mListModel.getFirstPage(false, new Callback<BaseArrayData<BrandEntity>>() {
            @Override
            public void onFinish(Entity<BaseArrayData<BrandEntity>> entity) {
                IParser<BaseArrayData<BrandEntity>> parser = entity.getParser();
                final BaseArrayData<BrandEntity> data = entity.getParsedData();
                if(parser.getIsOk() && data != null && data.getArray() != null) {
                    if (data.getArray().size() > 0) {
                        applyDataInfo(data.getArray());
                        gotoSuccessful();
                    } else {
                        gotoBlank();
                    }
                    return;
                }
                String msg = entity.getMessage();
                if(StringUtils.isEmpty(msg)) {
                    msg = getResources().getString(R.string.brand_getdata_fail);
                }
                MToast.showToastMessage(msg);
                gotoError();
            }

            @Override
            public void onError(Entity<BaseArrayData<BrandEntity>> entity) {
                gotoError();
                if(entity.getEntityStatus() != Entity.NETINVALID) {
                    MToast.showToastMessage(R.string.brand_getdata_fail);
                }
            }
        });
    }

    private void applyDataInfo(ArrayList<BrandEntity> array) {
        lstdata.setActionCallback(mActionCallbackProxy);
        lstdata.setDataSource(array);
    }
}
